package com.ruoyi.common.core.utils.business;

import com.ruoyi.common.core.constant.BusinessConstants;
import com.ruoyi.common.core.utils.zeroFill.ZeroFillUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * 实际业务工具类。
 */
public class BusinessUtils {

    /**
     * 获取当前年份
     */
    public static HashMap<String, Integer> getCurCalendar() {
        HashMap<String, Integer> res = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        res.put("year", calendar.get(Calendar.YEAR));
        res.put("month", calendar.get(Calendar.MONTH) + 1);
        res.put("date", calendar.get(Calendar.DATE));
        res.put("hour", calendar.get(Calendar.HOUR_OF_DAY));
        res.put("minute", calendar.get(Calendar.MINUTE));
        res.put("second", calendar.get(Calendar.SECOND));
        return res;
    }


    /**
     * 解析含有等级的编码 行政区划（10位）或机构（13位）使用此工具类
     *
     * @param codeStr 编码字符串
     *                6543220100
     * @return
     */
    public static HashMap<String, Object> getAddvAndOrgCodeInfo(String codeStr) {
        HashMap<String, Object> resHashMap = new HashMap<>();
        int grade = 0;
        String resCode = "";
        String headStr = "";
        if (codeStr.contains("ORG")) {
            codeStr = codeStr.replace("ORG", "");
            headStr = "ORG";
        }
        //循环五次 每次判断两位
        for (int i = 1; i < 6; i++) {
            //如果最后两位不为零，这接返回
            if (!codeStr.substring(codeStr.length() - 2).equals("00")) {
                grade = 4;
                resCode = headStr + codeStr;
                break;
            }
            String substring = codeStr.substring((i - 1) * 2, i * 2);
            if (substring.equals("00")) {
                grade = i - 2;
                resCode = headStr + codeStr.substring(0, (grade + 1) * 2);
                break;
            }
        }
        resHashMap.put("grade", grade);
        resHashMap.put("resCode", resCode);
        return resHashMap;
    }

    /**
     * 渠系编码解析工具
     *
     * @param codeStr
     * @return
     */
    public static HashMap<String, Object> getChanCodeInfo(String codeStr) {
        HashMap<String, Object> resHashMap = new HashMap<>();
        int grade = 0;
        String resCode = "";
        codeStr = codeStr.replace("CHA", "");

        for (int i = 1; i < 5; i++) {
            //如果最后两位不为零，这接返回
            if (!codeStr.substring(codeStr.length() - 3).equals("000")) {
                grade = 4;
                resCode = "CHA" + codeStr;
                break;
            }
            String substring = codeStr.substring((i - 1) * 3, i * 3);
            if (substring.equals("000")) {
                grade = i - 1;
                resCode = "CHA" + codeStr.substring(0, (grade) * 3);
                break;
            }
        }
        resHashMap.put("grade", grade);
        resHashMap.put("resCode", resCode);
        return resHashMap;
    }

    /**
     * 计算两个日期区间交集为几天
     *
     * @param beginDate
     * @param endDate
     * @param beginDate2
     * @param endDate2
     * @return
     * @throws Exception
     */
    public static int getDays(String beginDate, String endDate, String beginDate2, String endDate2) throws Exception {
        int res = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(beginDate));
        for (long d = cal.getTimeInMillis(); d <= sdf.parse(endDate).getTime(); d = get_D_Plaus_1(cal)) {
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(sdf.parse(beginDate2));
            for (long d2 = cal2.getTimeInMillis(); d2 <= sdf.parse(endDate2).getTime(); d2 = get_D_Plaus_1(cal2)) {
                if (sdf.format(d).equals(sdf.format(d2))) {
                    res = res + 1;
                }
            }
        }
        return res;
    }

    public static long get_D_Plaus_1(Calendar c) {
        c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + 1);
        return c.getTimeInMillis();
    }

    /**
     * 获取某月的最后一天 如201712结果为2017-12-30
     *
     * @param year
     * @param month
     * @return
     */
    public static String getMonthLastday(String year, String month) {
        LocalDate firstDayOfCurrentDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), 1);
        LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());
        return lastDayOfCurrentDate.toString();
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days + 1;
    }

    /**
     * 获取制定年月的前一年月份+1
     *
     * @param year
     * @param month
     * @return
     */
    public static String getStartYm(String year, String month) {
        String res;
        if ((Integer.valueOf(month) == 12)) {
            res = year + "01";
        } else {
            res = (Integer.valueOf(year) - 1) + ZeroFillUtils.zeroFill((Integer.valueOf(month) + 1), "00");
        }
        return res;
    }

    //    public static void main(String[] args) throws Exception {
////        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo("ORG6543220101");
////        System.out.println("结果 = " + addvAndOrgCodeInfo);
////        HashMap<String, Object> chanCodeInfo = BusinessUtils.getChanCodeInfo("CHA001005001000");
////        System.out.println("结果 = " + chanCodeInfo);
////        int days = getDays("2022-01-01", "2022-01-10", "2022-01-9", "2022-01-10");
////        System.out.println(days);
//////        BusinessConstants.TENSLIST
////        for (Map map:BusinessConstants.TENSLIST) {
////            System.out.println(map);
////        }
//        String startYm = getStartYm("2023", "10");
//        System.out.println(startYm);
//    }
// 密码长度不少于8位且至少包含大写字母、小写字母、数字和特殊符号中的四种
    public static final String password1 = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$";
    // 密码长度8-20位且至少包含大写字母、小写字母、数字或特殊符号中的任意三种
    public static final String password = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,20}$";


    public static void main(String[] args) {
        HashMap<String, Object> chanCodeInfo = BusinessUtils.getChanCodeInfo("CHA001000000000");
        System.out.println(">>>>>>>>>>>" + chanCodeInfo);
    }
}
